'use strict'

class record {
  constructor() {
    this.param = '/:date'
  }

  async get(c) {
    console.log(c.param)
    let date = decodeURI(c.param.date)
    let reta = await c.service.psql.query(`select * from record where record_date like '${date}' and record_user_id like '${c.box.user.openid}'`)
    c.send(reta.rows)
  }

  async post(c) {
    const data = c.body
    const user_id = c.box.user.openid
    console.log(data)
    let ret = await c.service.psql.query(`select * from record where record_date like '${data.time}' and record_user_id like '${c.box.user.openid}'`)
    if (!ret.rows[0]) {
      ret = await c.service.psql.query(`insert into record values('${data.text}', '${user_id}', '${data.mood}', '${data.weather}', '${data.time}', '${data.voice}')`)
      console.log(ret)
    } else {
      ret = await c.service.psql.query(`update record set record_content = '${data.text}', record_mood = '${data.mood}', record_weather = '${data.weather}', record_voice_url = '${data.voice}' where record_user_id like '${user_id}' and record_date like '${data.time}'`)
      console.log(ret)
    }
    console.log(ret)
    c.status(200).send('ok :)')
  }

}

module.exports = record
